import view as view
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from haystack.views import SearchView
from rest_framework import request
from rest_framework.generics import ListAPIView

from rest_framework.response import Response
from rest_framework_mongoengine.generics import GenericAPIView

from apps.recruit.models import Recruit
from apps.recruit.serialier import RecruitSerializer, EnterpriseSerializer

#
from apps.recruit.utils import PageNum



#搜索职位
# class RecruitSearchView(SearchView):
#
#     def create_response(self):
#
#         # 获取搜索结果
#         context = self.Recruit.object.filter(state=1)
#         data_list = []
#         for
#
#         # 拼接参数, 返回
#         return JsonResponse(data_list, safe=False)

#职位详情
class RecruitListView(ListAPIView):
    serializer_class = RecruitSerializer
    pagination_class = PageNum

    def get_queryset(self):

        return RecruitSerializer


#企业详情

class EnterpriseListView(ListAPIView):
    serializer_class = EnterpriseSerializer
    pagination_class = PageNum
    def get_queryset(self):
        return EnterpriseSerializer.data
#职位详情
class RecruitListView(ListAPIView):
    serializer_class = RecruitSerializer
    pagination_class = PageNum

    def get_queryset(self):
        return RecruitSerializer.data
#企业访问量
class EnterpriseVisitnumber(View):
    def put(self, request):
        enterprise = self.get_object()
        enterprise.visits += 1
        enterprise.save()
        return Response({"message":"更新成功","success":'true'})
#职位访问量
class RecruitVisitnumber(View):
    def put(self, request):
        recruit = self.get_object()
        recruit.visits += 1
        recruit.save()
        return Response({"message":"更新成功","success":'true'})
#收藏企业
class EnterpriseCollect(View):
    def collect(self, request):
        try:
            user = request.user
        except Exception as e:
            print(e)
        if  user.is_authenticated:
            enterprise = self.get_object()
            enterprise.users.add(user)
            enterprise.save()
            return Response({"message":"收藏成功","success":'true'})
        else:
            return Response({"message":"收藏成功","success":'true'})
#收藏职位
class RecruitCollect(View):
    def collect(self, request):
        try:
            user = request.user
        except Exception as e:
            print(e)
        if  user.is_authenticated:
            recruit = self.get_object()
            recruit.users.add(user)
            recruit.save()
            return Response({"message":"收藏成功","success":'true'})
        else:
            return Response({"message":"收藏成功","success":'true'})





